library(pacman)
pacman::p_load(tidyverse,data.table,broom,hrbrthemes,plyr,latex2exp,openxlsx,ggpubr,ggpmisc)
rm(list=ls())
################################################################################

#Abated emissions - PC
df = fread(file = 'results_pc/mps/dE.csv', header=FALSE)
df$conduct='perfect competition'
df_pc = df

#Abated emissions - IC
df = fread(file = 'results_ic/mps/dE.csv', header=FALSE)
df$conduct='imperfect competition'
df_ic = df

#Comparison: IC vs PC
df = rbind(df_ic[,list(V1,V2,conduct)],df_pc[,list(V1,V2,conduct)])[V1<=3,]

#Normalized scale
df$V2 = df$V2+1625000000
df$upperbound_C_T = df$V2/1000000
df$upperbound_C_D = 50*df$upperbound_C_T/1000
  
f1 = ggplot(data = df, aes(x=V1, y=upperbound_C_T, color=conduct))+
  geom_point(aes(shape = conduct, color=conduct), size=2.5) +
  geom_line(aes(linetype = conduct, color=conduct), linewidth=0.5) +
  labs(y=TeX('Mt $CO_2$e'), title = TeX("A. Targeting premium: T($\\tilde{\\sigma}$)")) +
  theme_minimal()+
  theme(axis.title.y = element_text(size=8, hjust=0.95),
        axis.title.x = element_text(size=8, hjust=1),
        axis.text.y = element_text(size=7),
        axis.text.x = element_text(size=7),
        plot.title = element_text(size=9, hjust=0.5, vjust=0),
        aspect.ratio = 0.8,
        panel.grid.minor = element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(size=8),
        legend.position = "inside",
        legend.key.width = unit(0.7,"cm"),
        legend.position.inside = c(0.65,0.2)
  )+
  scale_fill_manual(values=c("red4","gray25"))+
  scale_color_manual(values=c("red4","gray25"))+
  scale_x_continuous(name=TeX('dispersion of $CO_2$e intensity ( $\\tilde{\\sigma}^2 / \\sigma^2 ) $'), breaks=c(1,2,4,8))
f1_bw <- f1 + scale_color_grey(start=0, end=0.5)

f2 = ggplot(data = df, aes(x=V1, y=upperbound_C_D, color=conduct))+
  geom_point(aes(shape = conduct, color=conduct), size=2.5) +
  geom_line(aes(linetype = conduct, color=conduct), linewidth=0.5) +
  labs(y=TeX('billion USD'), title = TeX("B. Bound on enforcement cost: SCC x T$($\\tilde{\\sigma}$)")) +
  theme_minimal()+
  theme(axis.title.y = element_text(size=8, hjust=0.95),
        axis.title.x = element_text(size=8, hjust=1),
        axis.text.y = element_text(size=7),
        axis.text.x = element_text(size=7),
        plot.title = element_text(size=9, hjust=0.5, vjust=0.5),
        aspect.ratio = 0.8,
        panel.grid.minor = element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(size=8),
        legend.position = "inside",
        legend.key.width = unit(0.7,"cm"),
        legend.position.inside = c(0.65,0.2)
  )+
  scale_fill_manual(values=c("red4","gray25"))+
  scale_color_manual(values=c("red4","gray25"))+
  scale_x_continuous(name=TeX('dispersion of $CO_2$e intensity ( $\\tilde{\\sigma}^2 / \\sigma^2 ) $'), breaks=c(1,2,4,8))
f2_bw <- f2 + scale_color_grey(start=0, end=0.5)

figure=ggarrange(f1, f2, nrow=1, ncol=2)
ggsave(paste0("../../output/figures/figure6.pdf"), width = 5.5, height = 2.4, units='in')
ggsave(paste0("../../output/figures/figure6.eps"), width = 5.5, height = 2.4, units='in')

figure_bw=ggarrange(f1_bw, f2_bw, nrow=1, ncol=2)
ggsave(paste0("../../output/figures/figure6_bw.pdf"), width = 5.5, height = 2.4, units='in')
ggsave(paste0("../../output/figures/figure6_bw.eps"), width = 5.5, height = 2.4, units='in')

  

